package com.tianpengtech.front.web.passport;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.upload.UploadFile;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.im.kit.PushKit;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.task.Task;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.util.IpKit;
import com.tianpengtech.common.util.JsonUtil;
import com.tianpengtech.common.util.QiNiuUtil;
import com.tianpengtech.front.web.common.constant.Page;
import com.tianpengtech.front.web.common.controller.BaseController;
import com.tianpengtech.front.web.common.interceptor.PassportInterceptor;
import com.tianpengtech.front.web.login.LoginService;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Controller for account management.
 * @author Delicate
 */
public class PassportWebController extends BaseController {
    private static final PassportService srv = PassportService.me;
    private static final OpertionService opertionService = OpertionService.me;

    /**
     * User register
     */
    @Clear(PassportInterceptor.class)
    public void register(){
        renderTemplate(Page.REGISTER);
    }

    /**
     * 注册操作
     */
    @Clear(PassportInterceptor.class)
    @Before(PassportValidator.class)
    public void save() {
        Ret ret = srv.reg(getPara("mobile"), getPara("password"),User.USER_TYPE_PROV);
        ret.remove("id");
        renderJson(ret);
    }

    /**
     * 找回密码
     */
    @Clear(PassportInterceptor.class)
    @Before(PassportValidator.class)
    public void passwordBack() {
        Ret ret = srv.passwordBack(getPara("mobile"), getPara("password"),User.USER_TYPE_PROV);
        if (ret.isOk()) {
            renderText("ok");
        }else{
            renderJson(ret);
        }
    }

    /**
     * 发送验证码
     */
    @Clear(PassportInterceptor.class)
    @Before(PassportValidator.class)
    public void sendAuthcode(){
        String ip = IpKit.getRealIp(getRequest());
        String mobile = getPara("mobile");
        renderJson(srv.sendAuthCode(mobile,ip));
    }
    /**
     * 找回密码发送验证码
     */
    @Clear(PassportInterceptor.class)
    @Before(PassportValidator.class)
    public void sendAuthcodeByPasswordBack(){
        String ip = IpKit.getRealIp(getRequest());
        String mobile = getPara("mobile");
        renderJson(srv.sendAuthCodeByPasswordBack(mobile,ip,User.USER_TYPE_PROV));
    }
    /**
     * 验证 验证码
     */
    @Clear(PassportInterceptor.class)
    @Before(PassportValidator.class)
    public void authCode(){
        //validate里边进行验证 验证码是否正确
        renderJson(Ret.ok());
    }

    /**
     * User authenticate
     */
//    @Before(AuthValidator.class)
    public void auth(){
//        if ("GET".equals(getRequest().getMethod().toUpperCase())){
//            setAttr("domain", QiNiuUtil.DOMAIN);
//            renderTemplate("auth_form.html");
//        }else{
//            
//            
//           
//        }
    	try {
        	Authenticate auth = getModel(Authenticate.class);
            User user=getAttr(LoginService.loginAccountCacheName);
            auth.set(Authenticate.USER_ID,user.get(User.ID));
			srv.save(auth);
			PushKit.pushSystemMessageNow("用户【"+user.getStr(User.MOBILE)+"】提交了"+auth.getStr(Authenticate.COMPANY_NAME)+"的认证资料");
			renderJson(JsonUtil.printSuccess("提交认证资料成功"));
//			render("auth_working.html");
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
    }

    public void form(){
        setAttr("domain", QiNiuUtil.DOMAIN);
        renderTemplate("auth_form.html");
    }

    /**
     * 显示用户信息
     */
    public void detail(){
        String id=getPara("id");
        User user=null;
        if (StrKit.notBlank(id)){
            user=srv.findById(Integer.parseInt(id));
            user.remove(User.PASSWORD);
            user.remove(User.SALT);
            setAttr("user",user);
            Authenticate auth = user.getAuth();
            if (auth==null){
                redirect("/passport/form");
                return;
            }
            if (auth.getInt(Authenticate.STATUS)==Authenticate.STATUS_APPROVING){
                render("auth_working.html");
                return;
            }else if (auth.getInt(Authenticate.STATUS)==Authenticate.STATUS_FAILURE){
                String msg=opertionService.getAuthCheckLog(auth.get(Authenticate.ID)+"");
                msg = msg.substring(msg.indexOf(":")+1);
                setAttr("msg","原因是:"+msg);
                render("auth_failed.html");
                return;
            }
            setAttr("auth",auth);
        }else{
            renderJson(Ret.fail("msg","用户ID为空"));
            return;
        }

        render("auth_detail.html");
    }


    public void view(){
        String id=getPara("id");
        User user=null;
        if (StrKit.notBlank(id)){
            user=srv.findById(Integer.parseInt(id));
            user.remove(User.PASSWORD);
            user.remove(User.SALT);
        }
        setAttr("domain", QiNiuUtil.DOMAIN);
        setAttr("user",user);
        setAttr("auth",user.getAuth());
        render("auth_form_update.html");
    }

    /**
     * User forgot password
     */
    @Clear(PassportInterceptor.class)
    public void forget(){
        renderTemplate(Page.FORGET);
    }


    private String getUpDir(){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMDD");
        return "/"+sdf.format(new Date());
    }
    
    
    public void authWork(){
    	render("auth_working.html");
    }
}
